﻿using PermissionManager.Attributes;
using PermissionManager.BLL;
using PermissionManager.Model;
using PermissionManager.Models;
using PermissionsManager.BLL;
using PermissionsManager.Model;
using PermissionsManager.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace PermissionsManager.Controllers
{
    public class AdminController : Controller
    {

        AdminUserService adminUserService = new AdminUserService();
        BaseService<AdminModuleUser> baseService = new BaseService<AdminModuleUser>();
        ModuleService moduleService = new ModuleService();
       
        // GET: Admin        

        /// <summary>
        /// 管理员列表
        /// </summary>
        /// <returns></returns>
        [Describe("进入管理员页面")]
        public ActionResult Administrator()
        {
            //List<AdminModuleUser> adminUserRoles = adminUserService.Query();
            //ViewBag.adminInfoRoles = adminUserRoles;
            ViewBag.moduleList = moduleService.QueryList(a => true);            
            return View();
        }
        /// <summary>
        /// 得到组对应下的角色
        /// </summary>
        /// <param name="ModuleId"></param>
        /// <returns></returns>
        public JsonResult GetRoleByModule(int ModuleId)
        {
            GetJsonToTable<Role> getJson = new GetJsonToTable<Role>()
            {
                data= moduleService.GetRoleByModule(ModuleId)
            };
            return Json(getJson);
        }

        public JsonResult GetAdminUserInfo(int id) {
            AdminUser adminUser= adminUserService.Query(a => a.Id == id);            
            return Json(adminUser);
        }

        /// <summary>
        /// 获取管理员列表(分页)
        /// </summary>
        /// <param name="adminInfo"></param>
        /// <returns></returns>
        [HttpGet]
        public JsonResult QueryPageList(int page, int limit, string Name, string CreateTime)
        {
            DateTime date = new DateTime(188812);
            if (Name == null)
            {
                Name = "";
            }
            if (CreateTime != null && CreateTime != "")
            {
                date = DateTime.Parse(CreateTime);
            }

            List<AdminModuleUser> adminMoudles = adminUserService.Query(Name, date);
            GetJsonToTable<AdminModuleUser> getJsonTo = new GetJsonToTable<AdminModuleUser>();
            getJsonTo.code = 0;
            getJsonTo.msg = string.Empty;
            getJsonTo.data = adminMoudles.Skip((page - 1) * limit).Take(limit).ToList();
            getJsonTo.count = adminMoudles.Count;
            return Json(getJsonTo,JsonRequestBehavior.AllowGet);
        }


        /// <summary>
        /// 添加管理员
        /// </summary>
        /// <param name="adminUser"></param>
        /// <returns></returns>
        [Describe("新增管理员")]
        public JsonResult AddAdmin(AdminUser adminUser, int moduleId, int roleId)
        {
            ModuleRole moduleRole = new ModuleRole();
            moduleRole.ModuleId = moduleId;
            moduleRole.RoleId = roleId;
            int createUserId = AdminUserContext.context.UserInfo_no.Id;
            Opretation opretation = new Opretation();
            opretation.Success = adminUserService.AddAdmin(adminUser,createUserId,moduleRole);
            return Json(opretation);
        }



        /// <summary>
        /// 修改管理员
        /// </summary>
        /// <param name="adminUser"></param>
        /// <returns></returns>
        [Describe("修改管理员")]
        public JsonResult UpdateAdmin(AdminUser adminUser, int moduleId)
        {
            Opretation opretation = new Opretation();
            int createUserId = AdminUserContext.context.UserInfo_no.Id;
            opretation.Success = adminUserService.UpdateAdmin(adminUser,moduleId,createUserId);
            return Json(opretation);
        }


        /// <summary>
        /// 删除管理员
        /// </summary>
        /// <param name="adminUser"></param>
        /// <returns></returns>
        [Describe("删除管理员")]
        public JsonResult DeleteAdmin(int id, int moduleId)
        {
            Opretation opretation = new Opretation();
            opretation.Success = adminUserService.DeleteAdmin(id,moduleId);
            return Json(opretation);
        }


        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="adminUser"></param>
        /// <returns></returns>
        [Describe("删除管理员")]
        public JsonResult BatchDelete(string AdminId)
        {
            AdminId = AdminId.Substring(0, AdminId.Length - 1);
            Opretation opretation = new Opretation();
            string[] arr1 = AdminId.Split(','); //将字符串转换成数组
            int[] intArray = Array.ConvertAll<string, int>(arr1, s => int.Parse(s));
            opretation.Success = adminUserService.DeleteById(intArray);
            return Json(opretation);
        }

    }
}